'use strict';

const Controller = require('egg').Controller;
const { arrayGroup, uuid } = require('../../utils/index')

class AdminApiController extends Controller {
    /**
     * 添加接口界面
     * 
     * @memberof AdminApiController
     */
    async apiList() {
        let { ctx, service } = this;
        let parmas = ctx.query;
        let user = ctx.session.user;
        if(!user){
            ctx.redirect('/')
        }else{
        let result = await service.admin.adminApi.apiModel(parmas);
        let groupResult = arrayGroup(result.result)
        await ctx.render('admin/adminApi', { result: groupResult, project: result.project, user })
        }
    }


    /**
     *    // 查找分类
     * @memberof AdminApiController
     */
    async groupAction() {
        let { ctx, service } = this;
        let parmas = ctx.query;
        let res = await service.admin.adminApi.groupModel(parmas.system_id);
        if (res !== null) {
            ctx.body = {
                success: true,
                msg: '成功',
                result: res
            }
        } else {
            ctx.body = {
                success: false,
                msg: '查找失败'
            }
        }
    }

    async showApi() {
        let { ctx, service } = this;
        let parmas = ctx.query;
        let user = ctx.session.user;
        if (!user) {
            ctx.body='您还没有登陆...正在为您跳转'
        } else {

            let result = await service.admin.adminApi.showApiModel(parmas.api_id)

            if (result !== null) {
                ctx.body = {
                    success: true,
                    msg: '成功',
                    result
                }
            }
        }

    }

    /**
     * 添加接口
     * 
     * @memberof AdminApiController
     */
    async addApi() {
        let { ctx, service } = this;
        let parmas = ctx.request.body;
        let api_id = uuid(32);
        let res = await service.admin.adminApi.addApiModel({ ...parmas, api_id });

        this.resultStatus(res, ctx)

    }
    /**
     * 
     * 修改接口
     * @memberof AdminApiController
     */
    async updateApi() {
        let { ctx, service } = this;
        let parmas = ctx.request.body;
        let res = await service.admin.adminApi.updateApiModel(parmas);

        this.resultStatus(res, ctx)

    }
    /**
     * 
     * 删除接口
     * @memberof AdminApiController
     */
    async deleteApi() {
        let { ctx, service } = this;
        let parmas = ctx.query;

        let result = await service.admin.adminApi.deleteApiModel(parmas.api_id);
        console.log()
        this.resultStatus(result, ctx)
    }

    /**
     * 添加接口分组
     * 
     * @memberof AdminApiController
     */
    async addGroup() {
        let { ctx, service } = this;
        let parmas = ctx.request.body;
        let group_id = uuid(32);
        let result = await service.admin.adminApi.addGroupModel({ ...parmas, group_id })
        this.resultStatus(result, ctx)
    }
    async deleteGroup() {
        let { ctx, service } = this;
        let parmas = ctx.query;

        let result = await service.admin.adminApi.deleteGroupModel(parmas.group_id)
        this.resultStatus(result, ctx)
    }


    /**
     * 
     * 返回统一提示
     * @param {any} res 返回接口
     * @param {any} ctx 上下文
     * @memberof AdminApiController
     */
    resultStatus(res, ctx) {
        if (res.rows > 0) {
            ctx.body = {
                success: true,
                msg: '成功'
            }
        } else {
            ctx.body = {
                success: false,
                msg: '失败'
            }
        }
    }


}

module.exports = AdminApiController;